package conterlles

import (
	"day12_10_homework/models/mysql"
	"day12_10_homework/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CreateRealName(c *gin.Context) {
	realName := c.PostForm("realName")
	idCard := c.PostForm("idCard")
	value, _ := c.Get("userId")
	s := value.(string)
	userId, _ := strconv.Atoi(s)
	if pkg.Real(realName, idCard) {
		realNames := mysql.RealNames{
			UserId:   userId,
			RealName: realName,
			IdCard:   idCard,
		}
		err := realNames.CreateReal()
		if err != nil {
			c.JSON(http.StatusOK, JsonRes{
				Code:    500,
				Message: "实名认证失败",
				Data:    nil,
			})
			return
		}
		c.JSON(http.StatusOK, JsonRes{
			Code:    200,
			Message: "实名认证成功",
			Data:    realNames.ID,
		})
	} else {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "实名认证失败",
			Data:    nil,
		})
		return
	}
}
